/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lanSimulation.tests;


import java.io.StringWriter;
import junit.framework.TestCase;
import lanSimulation.LogManager;
import lanSimulation.internals.Document;
import lanSimulation.internals.Node;
import lanSimulation.internals.Packet;
import lanSimulation.internals.Printer;

/**
 *
 * @author farooq
 */
public class NodeTest extends TestCase{
    protected StringWriter writer;
    protected LogManager logMan;

    protected Node n1,n2,n3;

    public void setUp(){
        init();
    }
    
    private void init(){

           writer = new StringWriter(100);
           logMan = new LogManager(writer);

           n1 = new Node("n1",logMan);
           n2 = new Node("n2",logMan);
           n3 = new Node("n3",logMan);
           n1.nextNode_ = n2;
           n2.nextNode_ = n3;
           n3.nextNode_ = n1;

    }

    private void clearBuffer(){
        writer.getBuffer().delete(0, writer.getBuffer().length());
    }


    public void testBasicNode1() {

                Node n1 = new Node("n",new LogManager(new StringWriter(100)));
				
		assertEquals("name_", n1.name_, "n");
		assertEquals("nextNode_", n1.nextNode_, null);
                assertNotNull("logManager_", n1.getLogManager());
		n1.nextNode_ = n1;
		assertEquals("nextNode_ (after setting)", n1.nextNode_, n1);
	}

    public void testBasicNode2() {

                Node n1 = new Node("n1",new LogManager(new StringWriter(100)));
                Node n2 = new Node("n2",n1,new LogManager(new StringWriter(100)));

		assertEquals(n1.name_, "n1");
		assertEquals(n1.nextNode_, null);
                assertEquals(n2.name_,"n2");
                assertEquals(n2.nextNode_,n1);
                assertNotNull(n2.getLogManager());
		
	}


     public void testReceivePacket(){
             StringWriter writer = new StringWriter(100);             
             Node node = new Node("n",new LogManager(writer));
             node.receivePacket(new Packet(new Document("test document"),"n"));

             String test = "\tNode 'n' accepts broadcase packet.\n";
             assertEquals(test, writer.getBuffer().toString());
      }

     public void testSendPacket(){
             StringWriter writer = new StringWriter(100);
             Node node = new Node("n",new LogManager(writer));
             node.sendPacket(new Packet(new Document("test document"),"n"));

             String test = "\tNode 'n' passes packet on.\n";
             assertEquals(test, writer.getBuffer().toString());
      }

     public void testBroadcastPacket(){

               clearBuffer();
               n1.broadcastPacket(new Packet(new Document("test document"),"n2"));
               String test = "\tNode 'n1' accepts broadcase packet.\n";
               test += "\tNode 'n1' passes packet on.\n";
               assertEquals(test, writer.getBuffer().toString());               
     }

     
     public void testBroadcastPacket2(){

               clearBuffer();
               n1.broadcastPacket(new Packet(new Document("test document"),"n3"));

               String test = "\tNode 'n1' accepts broadcase packet.\n";
               test += "\tNode 'n1' passes packet on.\n";
               test += "\tNode 'n2' accepts broadcase packet.\n";
               test += "\tNode 'n2' passes packet on.\n";
               assertEquals(test, writer.getBuffer().toString());
     }

     public void testTransmitPrintPacket(){

               clearBuffer();
               n1.transmitPrintPacket(new Packet(new Document("Test Document"),"n3"));

               String test = "";
               test += "\tNode 'n1' passes packet on.\n";               
               test += "\tNode 'n2' passes packet on.\n";               
               test += ">>> Destinition is not a printer, print job cancelled.\n\n";

               assertEquals(test, writer.getBuffer().toString());

      }

     public void testPrintOn(){
                StringBuffer buf=new StringBuffer();
                Node node = new Node("n",null);
                node.printOn(buf);

                assertEquals("Node n [Node]",buf.toString());
     }
     
     public void testPrintXmlOn(){

                StringBuffer buf=new StringBuffer();
                Node node = new Node("n",null);
                node.printXmlOn(buf);

                assertEquals("<node>n</node>",buf.toString());
       }
}
